//
//  MCErrorManagement.m
//  MClient
//
//  Created by Zhouyi on 10-8-30.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCErrorManagement.h"

@interface MCErrorManagement(Private)
- (BOOL) iOS_SystemError:(NSString*)errCode;
@end

@implementation MCErrorManagement

static MCErrorManagement *errorManagementInstance = nil;

+ (MCErrorManagement *)instance {
	@synchronized(self) {
		if (errorManagementInstance == nil) {
			errorManagementInstance = [[self alloc] init]; // assignment not done here
		}
	}
	return errorManagementInstance;
}

- (void)doError:(NSString *)returnCode returnMessage:(NSString *)returnMessage
{
	// 停止等待光标转圈
	MClientAppDelegate *delegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
	[delegate stopActivityIndicatorView];
	
	if (![self iOS_SystemError:returnCode]) {
		// 弹出消息框提示错误信息。
		UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:([returnCode intValue]==100) ? @"异常提示" : @"系统提示" 
															message:returnMessage
														   delegate:nil 
												  cancelButtonTitle:nil 
												  otherButtonTitles:@"确定", nil];
		[alertView show];
		[alertView release];
	}
	if ([returnCode intValue] == 3) {
		MClientAppDelegate *delegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
		UINavigationController *navController = [delegate navController];
		[navController popToRootViewControllerAnimated:YES];
	}
}

+ (BOOL)showAuthMessage:(NSDictionary *)button
{
	NSDictionary* buttonStyle = [button objectForKey:@"style"];
	if ([[buttonStyle objectForKey:@"auth"] boolValue]) {
		NSString* auth_msg = [buttonStyle objectForKey:@"auth_msg"];
		
		// 弹出消息框提示错误信息。
		UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"消息提示" message:auth_msg delegate:nil 
												  cancelButtonTitle:nil 
												  otherButtonTitles:@"确定", nil];
		[alertView show];
		[alertView release];
		
		return YES;
	}
	
	return NO;
}

- (BOOL) iOS_SystemError:(NSString*)errCode {
//	switch ([errCode intValue]) {
//		case NSFileNoSuchFileError:// = 4,
//		case NSFileLockingError:// = 255,
//		case NSFileReadUnknownError = 256,
//		case NSFileReadNoPermissionError = 257,
//		case NSFileReadInvalidFileNameError = 258,
//		case NSFileReadCorruptFileError = 259,
//		case NSFileReadNoSuchFileError = 260,
//		case NSFileReadInapplicableStringEncodingError = 261,
//		case NSFileReadUnsupportedSchemeError = 262,
//		case NSFileReadTooLargeError = 263,
//		case NSFileReadUnknownStringEncodingError = 264,
//		case NSFileWriteUnknownError = 512,
//		case NSFileWriteNoPermissionError = 513,
//		case NSFileWriteInvalidFileNameError = 514,
//		case NSFileWriteInapplicableStringEncodingError = 517,
//		case NSFileWriteUnsupportedSchemeError = 518,
//		case NSFileWriteOutOfSpaceError = 640,
//		case NSKeyValueValidationError = 1024,
//		case NSFormattingError = 2048,
//		case NSUserCancelledError = 3072,
//		case NSFileErrorMinimum = 0,
//		case NSFileErrorMaximum = 1023,
//		case NSValidationErrorMinimum = 1024,
//		case NSValidationErrorMaximum = 2047,
//		case NSFormattingErrorMinimum = 2048,
//		case NSFormattingErrorMaximum = 2559,
//			
//			NSExecutableErrorMinimum = 3584,
//			NSExecutableNotLoadableError = 3584,
//			NSExecutableArchitectureMismatchError = 3585,
//			NSExecutableRuntimeMismatchError = 3586,
//			NSExecutableLoadError = 3587,
//			NSExecutableLinkError = 3588,
//			NSExecutableErrorMaximum = 3839,:
//			<#statements#>
//			break;
//		default:
//			break;
//	}
	if ([errCode intValue] == 1000) {
		return YES;
	}
	return NO;
}

@end
